from urllib2 import urlopen, URLError
import socket
import time
import sys

#print '\033[95m pyBicat 0.1 running \033[0m'

BASEURL = 'http://bicat.sfb-net.nl/cgi-bin/uls.pl?srvnum=99&event=tdetail&item='


class BicatDownloader(object):
    
    def __init__(self, ulsfile, sleeptime, request_timeout):
        self.bicat_file = ulsfile
        self.sleeptime = sleeptime
        self.request_timeout = request_timeout
        self.item_count = 0
        self.fails = 0
        self.requeue = ''
        
    def open_file(self):
        self.data = open(self.bicat_file, 'r')
        return self.data
        
    def open_url(self, l):
        key = l
        url = BASEURL + l
        try:
            if key == self.requeue:
                #sys.stdout.write('key: %s  | requeue: %s' % (key, self.requeue))
                sys.stdout.write("\x1B[31;40m[Item: %i ] reopen: %s\x1B[0m" % (self.item_count, url))
                document = urlopen(url, timeout=self.request_timeout)
            else:
                sys.stdout.write('[Item: %i ] open: %s' % (self.item_count, url))
                document = urlopen(url, timeout=self.request_timeout)
                #print 'wait %s seconds..' % self.sleeptime
                #time.sleep(self.sleeptime)
                self.item_count += 1
                
        except URLError:
            #print "\x1B[31;40m[request timed out]\x1B[0m"
            self.fails += 1
            self.requeue = key
            self.open_url(key)
            #print '[!Failed Attempts: %s ]' % self.fails
        except socket.timeout:
            #print "\x1B[31;40m[socket timed out]\x1B[0m"
            self.fails += 1
            
    def main(self):
        lines = self.open_file()
        for line in lines:
            self.open_url(line[2:])
        
    
downloader = BicatDownloader('titels.txt', 6, 10)
downloader.main()